﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace sow
{
	/// <summary>
	/// Summary description for emailcompose.
	/// </summary>
	public partial class emailadmin : System.Web.UI.Page
	{


		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			HttpCookie emailCookie = Request.Cookies["Email"];
			if(emailCookie == null)
				Response.Redirect("index.aspx");

			if (!EmailDataAccess.hasUserRole(emailCookie,5)) 
			{
				Response.Redirect("index.aspx");
			}

			if (!IsPostBack) 
			{
				GetEmailAccount();
				BindDataGrid();
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid_List.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_List_ItemCommand);
			this.DataGrid_List.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_List_ItemDataBound);

		}
		#endregion


		private void GetEmailAccount()
		{
			DataSet ds = EmailDataAccess.GetDataSet("EmailAccount", "");
			if(ds.Tables[0].Rows.Count > 0)
			{
				DataRow dr = ds.Tables[0].Rows[0];
				this.TextBox_EmailAccount.Text    = dr["EmailAccount"].ToString();
				this.TextBox_AccountPassword.Text = dr["AccountPassword"].ToString();
				this.TextBox_SmtpServer.Text      = dr["SmtpServer"].ToString();
				this.TextBox_LogDir.Text          = dr["LogDir"].ToString();
				if(dr["State"].ToString() == "1")
					this.CheckBox_State.Checked = true;
				else
					this.CheckBox_State.Checked = false;
				this.TextBox_EmailListSize.Text = dr["EmailListSize"].ToString();
				this.TextBox_EmailSize.Text     = dr["EmailSize"].ToString();
			}
		}


		private void BindDataGrid()
		{
			DataSet ds = new DataSet();
			DataColumn col = new DataColumn("LogFile");
			DataTable tbl = new DataTable("LogFile");
			tbl.Columns.Add(col);
			ds.Tables.Add(tbl);

			DataSet dsaccount = EmailDataAccess.GetDataSet("EmailAccount", "");
			if(dsaccount.Tables[0].Rows.Count > 0)
			{
				string[] file = System.IO.Directory.GetFiles(dsaccount.Tables[0].Rows[0]["LogDir"].ToString());
				for(int i = 0; i < file.Length; i++)
				{
					DataRow row = ds.Tables[0].NewRow();
					row["LogFile"] = file[i];
					ds.Tables[0].Rows.Add(row);
				}
			}

			this.DataGrid_List.DataSource = ds.Tables[0].DefaultView;
			this.DataGrid_List.DataBind();
		}


		private void DataGrid_List_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemIndex < 0)
				return;

			DataRowView drv = (DataRowView)e.Item.DataItem;
			Label label = (Label)e.Item.FindControl("Label_LogFile");
			label.Text = drv.Row["LogFile"].ToString();

			Button button = (Button)e.Item.FindControl("Button_Delete");
			button.CommandName = "DELETE";
			button.Attributes["onclick"] = "if(!confirm('确实要删除" + drv.Row["LogFile"].ToString() + "文件吗？')){return false;}";
			
			string loghref = label.Text.Substring(label.Text.LastIndexOf(@"\") + 1);
			string s = label.Text.Substring(0, label.Text.LastIndexOf(@"\"));
			loghref = s.Substring(s.LastIndexOf(@"\") + 1) + @"\" + loghref;
			e.Item.Cells[0].Text = "<a href='../" + loghref.Replace(@"\", @"/") + "' target=_blank>" + label.Text + "</a>";
		}

		
		private void DataGrid_List_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName == "DELETE")
			{
				Label label = (Label)e.Item.FindControl("Label_LogFile");
				System.IO.File.Delete(label.Text);

				this.BindDataGrid();
			}
		}

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			int listsize = 10;
			int emailsize = 50;
			try
			{
				listsize = Convert.ToInt32(this.TextBox_EmailListSize.Text);
			}
			catch{}
			try
			{
				emailsize = Convert.ToInt32(this.TextBox_EmailSize.Text);
			}
			catch{}

			string state = "";
			if(this.CheckBox_State.Checked)
				state = "1";
			EmailDataAccess.UpdateEmailAccount(this.TextBox_EmailAccount.Text.Trim(), 
											   this.TextBox_AccountPassword.Text.Trim(),
											   this.TextBox_SmtpServer.Text.Trim(),
											   this.TextBox_LogDir.Text.Trim(),
											   listsize,
											   emailsize,
											   state);

			this.BindDataGrid();
											 
		}










	}
}
